package com.lyp.mvpsupermanage.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.lyp.mvpsupermanage.App;
import com.lyp.mvpsupermanage.R;
import com.lyp.mvpsupermanage.utils.AppUtil;
import com.lyp.mvpsupermanage.widget.statusbar.StatusBarUtil;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import javax.inject.Inject;

import butterknife.ButterKnife;

/**
 * 作者:ChenXi 时间:2017/8/14 14: 12
 */

public abstract class BaseActivity<T extends BaseContract.BasePresenter> extends RxAppCompatActivity implements BaseContract.BaseView{


    @Inject
    protected T mPresenter;
    protected Context mContext;//上下文环境
    protected Toolbar mToolbar;//Toolbar
    protected boolean mBack = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        mContext = this;
        mToolbar = ButterKnife.findById(this, R.id.toolbar);
        ButterKnife.bind(this);
        initStatusBar();//状态栏
        initInject();
        initPresenter();
        initVariables();
        App.getInstance().addActivity(this);
        if (mToolbar != null) {
            //初始化Toolbar
            initToolbar();
            //让组件支持Toolbar
            setSupportActionBar(mToolbar);
            if (mBack) mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
        initWidget();
        initDatas();
    }


    /**
     * 注入依赖
     */
    protected void initInject() {

    }

    protected void initRecyclerView() {

    }

    /**
     * 完成请求
     */
    protected void finishTask() {
    }

    /**
     * 初始化StatusBar/状态栏
     */
    protected void initStatusBar() {
        /*if (mDrawerLayout != null) {
            StatusBarUtil.setColorNoTranslucentForDrawerLayout((Activity) mContext, mDrawerLayout, AppUtil.getColor(R.color.colorPrimary));
        } else {*/
        StatusBarUtil.setColorNoTranslucent((Activity) mContext, AppUtil.getColor(R.color.colorPrimary));
        // }
    }

    /**
     * 初始化Presenter
     */
    private void initPresenter() {
        if (mPresenter != null) mPresenter.attachView(this);
    }

    /**
     * 初始化Toolbar
     */
    protected void initToolbar() {
        if (mBack) mToolbar.setNavigationIcon(R.drawable.ic_clip_back_white);
    }

    /**
     * 布局文件
     *
     * @return 布局文件
     */
    protected abstract
    @LayoutRes
    int getLayoutId();

    /**
     * 初始化控件
     */
    protected void initWidget() {
    }

    /**
     * 加载数据
     */
    protected void loadData() {
    }

    /**
     * 初始化数据
     */
    protected void initDatas() {
        loadData();
    }

    /**
     * 初始化变量
     */
    protected void initVariables() {
    }

    /**
     * 隐藏View
     *
     * @param views 视图
     */
    protected void gone(final View... views) {
        if (views != null && views.length > 0) {
            for (View view : views) {
                if (view != null) {
                    view.setVisibility(View.GONE);
                }
            }
        }
    }

    /**
     * 显示View
     *
     * @param views 视图
     */
    protected void visible(final View... views) {
        if (views != null && views.length > 0) {
            for (View view : views) {
                if (view != null) {
                    view.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    /**
     * 隐藏View
     *
     * @param id
     */
    protected void gone(final @IdRes int... id) {
        if (id != null && id.length > 0) {
            for (int resId : id) {
                View view = $(resId);
                if (view != null)
                    gone(view);
            }
        }

    }

    /**
     * 显示View
     *
     * @param id
     */
    protected void visible(final @IdRes int... id) {
        if (id != null && id.length > 0) {
            for (int resId : id) {
                View view = $(resId);
                if (view != null)
                    visible(view);
            }
        }
    }

    private View $(@IdRes int id) {
        View view;
        if (this != null) {
            view = this.findViewById(id);
            return view;
        }
        return null;
    }
}
